package main

import (
	"fmt"
	"go-web/src/db-demo/utils"
)

func main() {

	users, err := FindAllUser()
	if err != nil {
		fmt.Printf("查询用户失败: %v\n", err)
	} else {
		for _, user := range users {
			fmt.Printf("User: %v\n", user)
		}
	}
}

type User struct {
	Id       int
	Username string
	Password string
	Email    string
}

func FindAllUser() (user []*User, err error) {
	sqlStr := "select id, username, password, email from go_user"
	stmt, err := utils.Db.Prepare(sqlStr)
	if err != nil {
		return nil, err
	}

	rows, err := stmt.Query()

	if err != nil {
		return nil, err
	}

	var users []*User

	for rows.Next() {
		var id int
		var username string
		var password string
		var email string
		err = rows.Scan(&id, &username, &password, &email)
		if err != nil {
			return nil, err
		}

		users = append(users, &User{
			Id:       id,
			Username: username,
			Password: password,
			Email:    email,
		})
	}

	return users, nil
}
